/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.system.service.impl;

import cn.hutool.core.util.StrUtil;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.exception.EntityExistException;
import me.zhengjie.modules.system.domain.Menu;
import me.zhengjie.modules.system.domain.Role;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.repository.RoleRepository;
import me.zhengjie.modules.system.repository.UserRepository;
import me.zhengjie.modules.system.service.RoleService;
import me.zhengjie.modules.system.service.dto.RoleDto;
import me.zhengjie.modules.system.service.dto.RoleQueryCriteria;
import me.zhengjie.modules.system.service.dto.RoleSmallDto;
import me.zhengjie.modules.system.service.dto.UserDto;
import me.zhengjie.modules.system.service.mapstruct.RoleMapper;
import me.zhengjie.modules.system.service.mapstruct.RoleSmallMapper;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.RedisUtils;
import me.zhengjie.utils.ValidationUtil;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Zheng Jie
 * @date 2018-12-03
 */
@Service
@RequiredArgsConstructor
@CacheConfig(cacheNames = "role")
public class RoleServiceImpl implements RoleService {

  private final RoleRepository roleRepository;
  private final RoleMapper roleMapper;
  private final RoleSmallMapper roleSmallMapper;
  private final RedisUtils redisUtils;
  private final UserRepository userRepository;

  @Override
  public List<RoleDto> queryAll() {
    Sort sort = Sort.by(new Order(Sort.Direction.ASC, "level"));
    return roleMapper.toDto(roleRepository.findAll(sort));
  }

  @Override
  public List<RoleDto> queryAll(RoleQueryCriteria criteria) {
    return roleMapper.toDto(roleRepository.findAll(
        (root, criteriaQuery, criteriaBuilder) -> QueryHelp
            .getPredicate(root, criteria, criteriaBuilder)));
  }

  @Override
  public Object queryAll(RoleQueryCriteria criteria, Pageable pageable) {
    Page<Role> page = roleRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp
        .getPredicate(root, criteria, criteriaBuilder), pageable);
    return PageUtil.toPage(page.map(roleMapper::toDto));
  }

  @Override
  @Cacheable(key = "'id:' + #p0")
  @Transactional(rollbackFor = Exception.class)
  public RoleDto findById(long id) {
    Role role = roleRepository.findById(id).orElseGet(Role::new);
    ValidationUtil.isNull(role.getId(), "Role", "id", id);
    return roleMapper.toDto(role);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void create(Role resources) {
    if (roleRepository.findByName(resources.getName()) != null) {
      throw new EntityExistException(Role.class, "username", resources.getName());
    }
    roleRepository.save(resources);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void update(Role resources) {
    Role role = roleRepository.findById(resources.getId()).orElseGet(Role::new);
    ValidationUtil.isNull(role.getId(), "Role", "id", resources.getId());

    Role role1 = roleRepository.findByName(resources.getName());

    if (role1 != null && !role1.getId().equals(role.getId())) {
      throw new EntityExistException(Role.class, "username", resources.getName());
    }
    role.setName(resources.getName());
    role.setDescription(resources.getDescription());
    role.setDataScope(resources.getDataScope());
    role.setDepts(resources.getDepts());
    role.setLevel(resources.getLevel());
    roleRepository.save(role);
    // 更新相关缓存
    delCaches(role.getId());
  }

  @Override
  public void updateMenu(Role resources, RoleDto roleDTO) {
    Role role = roleMapper.toEntity(roleDTO);
    // 清理缓存
    List<User> users = userRepository.findByRoleId(role.getId());
    Set<Long> userIds = users.stream().map(User::getId).collect(Collectors.toSet());
    redisUtils.delByKeys("menu::user:", userIds);
    redisUtils.del("role::id:" + resources.getId());
    // 更新菜单
    role.setMenus(resources.getMenus());
    roleRepository.save(role);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void untiedMenu(Long menuId) {
    // 更新菜单
    roleRepository.untiedMenu(menuId);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void delete(Set<Long> ids) {
    for (Long id : ids) {
      // 更新相关缓存
      delCaches(id);
    }
    roleRepository.deleteAllByIdIn(ids);
  }

  @Override
  public List<RoleSmallDto> findByUsersId(Long id) {
    return roleSmallMapper.toDto(new ArrayList<>(roleRepository.findByUserId(id)));
  }

  @Override
  public Integer findByRoles(Set<Role> roles) {
    Set<RoleDto> roleDtos = new HashSet<>();
    for (Role role : roles) {
      roleDtos.add(findById(role.getId()));
    }
    return Collections.min(roleDtos.stream().map(RoleDto::getLevel).collect(Collectors.toList()));
  }

  @Override
  @Cacheable(key = "'auth:' + #p0.id")
  public List<GrantedAuthority> mapToGrantedAuthorities(UserDto user) {
    Set<String> permissions = new HashSet<>();
    // 如果是管理员直接返回
    if (user.getIsAdmin()) {
      permissions.add("admin");
      return permissions.stream().map(SimpleGrantedAuthority::new)
          .collect(Collectors.toList());
    }
    Set<Role> roles = roleRepository.findByUserId(user.getId());
    permissions = roles.stream().flatMap(role -> role.getMenus().stream())
        .filter(menu -> StrUtil.isNotBlank(menu.getPermission()))
        .map(Menu::getPermission).collect(Collectors.toSet());
    return permissions.stream().map(SimpleGrantedAuthority::new)
        .collect(Collectors.toList());
  }

  @Override
  public void download(List<RoleDto> roles, HttpServletResponse response) throws IOException {
    List<Map> list = new ArrayList<>();
    for (RoleDto role : roles) {
      Map<String, Object> map = new LinkedHashMap<>();
      map.put("角色名称", role.getName());
      map.put("角色级别", role.getLevel());
      map.put("描述", role.getDescription());
      map.put("创建日期", role.getCreateTime());
      list.add(map);
    }
    FileUtil.downloadExcel(list, response);
  }

  /**
   * 清理缓存
   *
   * @param id /
   */
  public void delCaches(Long id) {
    List<User> users = userRepository.findByRoleId(id);
    Set<Long> userIds = users.stream().map(User::getId).collect(Collectors.toSet());
    redisUtils.delByKeys("data::user:", userIds);
    redisUtils.delByKeys("menu::user:", userIds);
    redisUtils.delByKeys("role::auth:", userIds);
  }

  @Override
  public void verification(Set<Long> ids) {
    if (userRepository.countByRoles(ids) > 0) {
      throw new BadRequestException("所选角色存在用户关联，请解除关联再试！");
    }
  }
}
